//
//  CZMainTabBarController.swift
//  SinaWeiBo
//
//  Created by 刘幼寒 on 5/11/16.
//  Copyright © 2016 刘幼寒. All rights reserved.
//

import UIKit

class CZMainTabBarController: UITabBarController
{

    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        //定义一个闭包
        let composeCallBack = {() -> () in
            print("123")
        }
        
        
//        tabBar.tintColor = UIColor.orangeColor()
        let mainTabBar = CZMainTabBar()
        //get,只读属性，没有set方法，=赋值调用的是set方法，只读属性没有了set就不能用=直接赋值
//        tabBar = mainTabBar
//        UITabBar有个tabBar属性
        //使用KVC，KVC会先找set方法，如果没有set方法会找_tabBar成员变量，KVC使用运行时
        setValue(mainTabBar, forKey: "tabBar")
        
        setupChilderViewController()
        
        mainTabBar.composeCallBack = composeCallBack
        

    }
    
    func setupChilderViewController()
    {
//        //首页
//        let homeVC = CZHomeViewController()
//        homeVC.title = "首页"//不仅会在tabBar上面显示，还会在导航栏上显示
//        //        homeVC.tabBarItem.title = "首页"//只会显示在tabBar上面
//        homeVC.tabBarItem.image = UIImage(named: "tabbar_home")
//        homeVC.tabBarItem.selectedImage = UIImage(named: "tabbar_home_selected")
//        //包装导航控制器
//        //        let navigationController = UINavigationController(rootViewController: homeVC)
//        self.addChildViewController(UINavigationController(rootViewController: homeVC))
//        
//        //消息
//        let messageVC = CZMessageViewController()
//        messageVC.title = "消息"
//        messageVC.tabBarItem.image = UIImage(named: "tabbar_message_center")
//        messageVC.tabBarItem.selectedImage = UIImage(named: "tabbar_message_center_selected")
//        self.addChildViewController(UINavigationController(rootViewController: messageVC))
//        
//        //发现
//        let discoverVC = CZDiscoverViewController()
//        discoverVC.title = "发现"
//        discoverVC.tabBarItem.image = UIImage(named: "tabbar_discover")
//        discoverVC.tabBarItem.image = UIImage(named: "tabbar_discover_selected")
//        self.addChildViewController(UINavigationController(rootViewController: discoverVC))
//        
//        //我
//        let profileVC = CZProfileViewController()
//        profileVC.title = "我"
//        profileVC.tabBarItem.image = UIImage(named: "tabbar_profile")
//        profileVC.tabBarItem.selectedImage = UIImage(named: "tabbar_profile_selected")
//        self.addChildViewController(UINavigationController(rootViewController: profileVC))
        
        //首页
        addChildViewController(CZHomeViewController(), title: "首页", imageName: "tabbar_home")
        //消息
        addChildViewController(CZMessageViewController(), title: "消息", imageName: "tabbar_message_center")
        //发现
        addChildViewController(CZDiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        //我
        addChildViewController(CZProfileViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    func addChildViewController(childController: UITableViewController, title: String, imageName: String)
    {
        childController.title = title//不仅会在tabBar上面显示，还会在导航栏上显示
        //        homeVC.tabBarItem.title = "首页"//只会显示在tabBar上面
        childController.tabBarItem.image = UIImage(named: imageName)
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        //包装导航控制器
        self.addChildViewController(UINavigationController(rootViewController: childController))
    }

}
